package net.lgpage.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

/*
 * 流处理工具类
 */
public class StreamTool {
	
	private final static String TAG = "StreamTool";
	//将流转换成btye数组
	public static byte[] getBytes(InputStream is){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		try {
			while((len = is.read(buffer))!=-1){
				bos.write(buffer, 0, len);
			}
			is.close();
			bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null)
				try {
					is.close();
				} catch (IOException e) {
					Log.e(TAG, "ioexception");
				}
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					Log.e(TAG, "ioexception");
				}
			}
		}
		byte[] result = bos.toByteArray();
		return  result;
	}
	
	public static String toString(InputStream is){
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		StringBuilder sb=new StringBuilder();
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line+"\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}
}


